<?php

namespace AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class BaseController extends Controller
{
    protected function renderWithRole(Request $request, $view, array $parameters = array(), Response $response = null)
    {
        $user = $this->getUser();
        $username = $user->getUsername();
        $roles = $user->getRoles();
        $em = $this->getDoctrine()->getManager();

        if (in_array($this->getParameter('role_root'), $roles)) {
            $menu = $em->getRepository('AdminBundle:AuthContentType')->loadMenuWithRoot();
        } else {
            $menu = $em->getRepository('AdminBundle:AuthContentType')->loadMenuWithUser($username);
        }
        return parent::render($view, array_merge($parameters, [
            'rabc_menu' => $menu, 'route' => $request->attributes->get('_route', '')]),
            $response);
    }

    protected function render($view, array $parameters = array(), Response $response = null)
    {
        if (0) {
            return $this->renderWithRole($view, $parameters, $response);
        }
        return parent::render($view, $parameters, $response);
    }
}
